﻿using ICMS.PluginInterfaces;

using commonImportPlugin.Factorys;
using System;
using System.Collections.Generic;
using System.IO;
using commonImportPlugin.Config;
using System.Text;
using commonImportPlugin.Common;
using commonImportPlugin.Configs.TrackFile;

namespace commonImportPlugin.FilesCore.TrackFile
{
    /// <summary>
    /// 《普通模式通用磁条数据解析核心类》
    /// 该模式支持磁条数据以行为单位，行以
    /// 特定分隔符分割的数据。该模式支持70%
    /// 以上磁条数据的解析。
    /// </summary>
    public class TrackCoreGeneralClass : TrackFileCore
    {
        static TrackFileConfig config = null;
        TrackBase trackBase = new TrackBase();
        CommonUtil commonUtil = CommonUtil.GetInstance();
        static Context context;
        public TrackCoreGeneralClass GetInstance()
        {
            if (isGetConfigSucceed())
            {
                context = Context.GetConfig();
                return new TrackCoreGeneralClass();
            }
            return null;
        }
        private bool isGetConfigSucceed()
        {
            config = TrackFileConfig.GetConfig();
            if (config == null)
                return false;
            else
                return true;
        }
        /// <summary>
        /// 获取所有的磁条数据
        /// </summary>
        /// <param name="trackFilePaths">磁条数据文件集</param>
        /// <param name="trackCounts">成功获取到的磁条数据量</param>
        /// <returns>磁条数据量</returns>
        public override List<ICData> GetTrackAllDatas(List<string> trackFilePaths, out int trackCounts)
        {
            List<ICData> allTrackIcDatas = new List<ICData>();
            List<ICData> finalDatas = new List<ICData>();
            foreach (string trackFile in trackFilePaths)
            {
                trackBase.getFileTrackDatas(allTrackIcDatas, trackFile,config);
            }
            ////////////////过滤数据////////////////
            #region 过滤
            if (!string.IsNullOrEmpty(config.filterItem))
            {
                finalDatas = commonUtil.FilterDatasWithFile(allTrackIcDatas, context.succeedLogFilePth, config.filterItem);
            }
            else
                finalDatas = allTrackIcDatas;
            #endregion

            trackCounts = finalDatas.Count;
            return finalDatas;
        }
        /// <summary>
        /// 获取所有的磁条数据量
        /// </summary>
        /// <param name="trackFilePaths">磁条文件</param>
        /// <returns>磁条数据量</returns>
        public override int GetTrackAllCounts(List<string> trackFilePaths)
        {
            int allTrackCount = 0;
            foreach (string trackFile in trackFilePaths)
            {
                string[] originTrackLines = null;
                if (!File.Exists(trackFile))
                    throw new Exception("磁条文件不存在！");
                originTrackLines = File.ReadAllLines(trackFile, Encoding.GetEncoding(config.encoding));
                int eachFileCount = originTrackLines.Length - config.dataIndex + 1;
                allTrackCount += eachFileCount;

            }
            return allTrackCount;
        }
        public override bool CheckTrackData()
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 《正则模式通用磁条数据解析核心类》
    /// 该模式支持磁条数据以行为单位，行以
    /// 特定分隔符分割的数据。该模式支持70%
    /// 以上磁条数据的解析。
    /// </summary>
    public class TrackCoreRegexClass : TrackFileCore
    {
        static TrackFileConfigRegex config = null;
        TrackBase trackBase = new TrackBase();
        CommonUtil commonUtil = CommonUtil.GetInstance();
        static Context context;
        public TrackCoreRegexClass GetInstance()
        {
            if (IsGetConfigSucceed())
            {
                context = Context.GetConfig();
                return new TrackCoreRegexClass();
            }
            return null;
        }
        private bool IsGetConfigSucceed()
        {
            config = TrackFileConfigRegex.GetConfig();
            if (config == null)
                return false;
            else
                return true;
        }
        /// <summary>
        /// 获取所有的磁条数据
        /// </summary>
        /// <param name="trackFilePaths">磁条文件</param>
        /// <param name="trackCounts">磁条数据量</param>
        /// <returns>磁条数据</returns>
        public override List<ICData> GetTrackAllDatas(List<string> trackFilePaths, out int trackCounts)
        {
            List<ICData> allTrackIcDatas = new List<ICData>();
            List<ICData> finalDatas = new List<ICData>();
            foreach (string trackFile in trackFilePaths)
            {
                trackBase.getFileTrackDatas(allTrackIcDatas, trackFile, config);
            }

            ////////////////过滤数据////////////////
            #region 过滤
            if (!string.IsNullOrEmpty(config.filterItem))
            {
                finalDatas = commonUtil.FilterDatasWithFile(allTrackIcDatas, context.succeedLogFilePth, config.filterItem);
            }
            else
                finalDatas = allTrackIcDatas;
            #endregion

            trackCounts = finalDatas.Count;
            return finalDatas;
        }
        /// <summary>
        /// 获取所有的磁条数据量
        /// </summary>
        /// <param name="trackFilePaths">磁条数据文件集</param>
        /// <returns>磁条数据量</returns>
        public override int GetTrackAllCounts(List<string> trackFilePaths)
        {
            int allTrackCount = 0;

            foreach (string trackFile in trackFilePaths)
            {
                string[] originTrackLines = null;
                if (!File.Exists(trackFile))
                    throw new Exception("磁条文件不存在！");
                originTrackLines = File.ReadAllLines(trackFile, Encoding.GetEncoding(config.encoding));
                int eachFileCount = originTrackLines.Length - config.dataIndex + 1;
                allTrackCount += eachFileCount;
            }
            return allTrackCount;
        }
        public override bool CheckTrackData()
        {
            throw new NotImplementedException();
        }
    }
}
